package com.atguigu.edumsm.service.impl;

import com.atguigu.edumsm.service.MsmService;
import com.atguigu.edumsm.utils.RandomUtil;
import com.atguigu.edumsm.utils.TxCode;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author 25169
 */
@Service
public class MsmServiceImpl implements MsmService {

    private static final Logger logger = LoggerFactory.getLogger(MsmServiceImpl.class);
    @Autowired
    RedisTemplate<String,String> redisTemplate;

    @Override
    public boolean send(String phone) {
        //生成四位数验证码交腾讯云发送
        String code = RandomUtil.getFourBitRandom();
        //几分钟失效
        String min="5";

        //发送验证码
        boolean res = TxCode.SendCode("+86" + phone,code);

        if (res){
            //发送成功后将验证码放到Redis中设置存活时间5分钟
            redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
            return true;
        }else {
            throw new GuliException(20001,"验证码发送失败");
        }


    }
}
